package org.duang.logs.db;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.duang.config.Config;

import ch.qos.logback.classic.Level;

public class ToolUtils {

	private static String LOCAL_HOST = "";
	private static String LOCAL_PROT = "";
	private static String PRODUCT_CODE = "";

	public static String builderESId() {
		return java.util.UUID.randomUUID().toString().replace("-", "").trim();
	}

	// private static java.text.SimpleDateFormat sdf = new
	// java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static Date getDateTime(long datetime) {
		try {
			// return sdf.parse(sdf.format(new Date(datetime)));
			return new Date(datetime);
		} catch (Exception e) {
			return new Date(datetime);
		}
	}

	public static String getLocalHost() {
		if (!"".equals(LOCAL_HOST))
			return LOCAL_HOST;
		try {
			InetAddress addr = InetAddress.getLocalHost();
			return addr.getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String getLocalPort() {
		if (!"".equals(LOCAL_PROT))
			return LOCAL_PROT;
		/**
		 * 设置需要的Key，例如Tomcat服务器：
		 * 
		 * 编辑文件${tomact-dir}/bin/catalina.bat, 添加以下内容即可： set JAVA_OPTS=-D<key>=
		 * "<value>" 如： set JAVA_OPTS=-Dreyo.localPort="8081"
		 * 
		 * 然后在程序取出 System.getProperty("reyo.localPort")了
		 */
		return "8080";
	}

	public static String getProductCode() {
		if (!"".equals(PRODUCT_CODE))
			return PRODUCT_CODE;
		try {
			PRODUCT_CODE = Config.getProductCode();
		} catch (Exception e) {
			PRODUCT_CODE = "duang-logs";
			System.out.println("#############没有设置产品代码,请设置! 现默认为: " + PRODUCT_CODE);
		}
		return PRODUCT_CODE;
	}

	public static Level getLevel() {
		try {
			return Level.toLevel(Config.getLogBackLevel());
		} catch (Exception ex) {
			return Level.INFO;
		}
	}

	/**
	 * 是否控制台输出日志
	 * 
	 * @return
	 */
	public static boolean isAdditive() {
		try {
			if (Config.getDebug()) {
				return true;
			}
		} catch (Exception ex) {
		}
		return (getLocalHost().startsWith("10")) ? false : true;
	}

	public static String getIndexName() {
		try {
			return Config.getElasticSearchIndexName();
		} catch (Exception e) {
			return "syt";
		}
	}

	public static String getTypeName() {
		try{
		return Config.getElasticSearchTypeName();
		}catch(Exception ex) {
			return "sys_logback";
		}
	}
	
	
	public static String getMongoHost() {
		try {
			return Config.getMongoDBHost();
		} catch (Exception e) {
			throw new NullPointerException(" Get MongoDB Host is Fail: " + e.getMessage());
		}
	}

	public static int getMongoPort() {
		try{
			return Integer.parseInt(Config.getMongoDBPort());
		} catch(Exception e) {
			throw new NullPointerException(" Get MongoDB Port is Fail: " + e.getMessage());
		}
	}

	public static List<String> getEsHost() {
		try {
			ArrayList<String> hostList = new java.util.ArrayList<String>();
			String replicaset =  Config.getElasticSearchReplicaset();			
			if(null != replicaset && replicaset.length() > 0) {
				String[] items = replicaset.split(",");
				for(String item : items) {
					String[] itemArray = item.split(":");
					hostList.add(itemArray[0]);
				}
			}
			return hostList;
		} catch (Exception e) {
			throw new NullPointerException(" Get ElasticSearch Host is Fail: " + e.getMessage());
		}
	}

	public static int getEsPort() {
		try{
			return Config.getElasticSearchPort();
		} catch(Exception e) {
			return 9300;
		}
	}
	public static String getStackTraceAsString(Exception e) {
		StringWriter stringWriter = new StringWriter();
		e.printStackTrace(new PrintWriter(stringWriter));
		return stringWriter.toString();
	}
}
